import { loopArrayChildren } from '@/utils';

/**
 * 只用于公共管理组件的 按照组织机构人搜索使用,在表单改变的api中使用了
 * @param changeKey     改变的表单字段name
 * @param deptSearchArr   组织机构的数据
 * @returns
 */
export const handleFilterPayload = (changeKey: any, deptSearchArr: any) => {
  let formChangeText: string = Object?.keys(changeKey)?.length > 0 ? Object?.keys(changeKey)?.toString() : ''; //提取出来改变的表单名字
  let changeType: boolean = handleOtehrFormBool(formChangeText, deptSearchArr);
  if (changeType) {
    let orgFormName: any = []; //保存组织下拉框的 表单name字段
    let orgDataList: any = []; //保存组织机构搜索组织的数据
    deptSearchArr?.forEach((itemDel: any) => {
      if (itemDel?.componentType == 'DeptAndUser') {
        if (formChangeText == `${itemDel?.name}Dept`) {
          orgFormName[formChangeText] =
            (changeKey?.[formChangeText] || changeKey?.[formChangeText] >= 0) && changeKey?.[formChangeText]?.length > 0
              ? handleCheckDept(changeKey?.[formChangeText])
              : null;
        }
        orgDataList = itemDel?.dataSource;
      }
    });

    let loopOrgDataList: any = loopArrayChildren(orgDataList); //平铺组织数据,每个节点都含有该节点点的子数据
    let selectList = loopOrgDataList?.filter((item: any) => orgFormName?.[formChangeText]?.includes(item?.id)); //找到点击的树节点
    let userListPayload = handleGetId(selectList); //处理点击选中节点,传参的数据
    let otherForm = handleOtehrForm(formChangeText, deptSearchArr); //处理 点击的节点返回的 岗位部门,人的表单字段

    let treeObj: any = {};
    if (userListPayload && JSON.stringify(userListPayload) != '{}') {
      if (userListPayload && userListPayload?.userCompanyID) {
        treeObj[otherForm?.COMP] = userListPayload?.userCompanyID;
      } else {
        treeObj[otherForm?.COMP] = [];
      }
      if (userListPayload && userListPayload?.userDepartmentID) {
        treeObj[otherForm?.DEPT] = userListPayload?.userDepartmentID;
      } else {
        treeObj[otherForm?.DEPT] = [];
      }
      if (userListPayload && userListPayload?.userPositionID) {
        treeObj[otherForm?.POS] = userListPayload?.userPositionID;
      } else {
        treeObj[otherForm?.POS] = [];
      }
    } else {
      //处理去除搜索选中的值后,为null,最后传入表单的时候,会清除值为null的参数
      treeObj[otherForm?.COMP] = null;
      treeObj[otherForm?.DEPT] = null;
      treeObj[otherForm?.POS] = null;
    }

    // if (selectList?.[0]?.textType == 0) {
    //   treeObj[otherForm?.COMP] = userListPayload?.userCompanyID
    // } else if (selectList?.[0]?.textType == 1) {
    //   treeObj[otherForm?.DEPT] = userListPayload?.userDepartmentID
    // } else if (selectList?.[0]?.textType == 2) {
    //   treeObj[otherForm?.POS] = userListPayload?.userPositionID
    // } else {  //处理去除搜索选中的值后,为null,最后传入表单的时候,会清除值为null的参数
    //   treeObj[otherForm?.COMP] = null
    //   treeObj[otherForm?.DEPT] = null
    //   treeObj[otherForm?.POS] = null
    // }
    return treeObj;
  } else {
    let treeObj: any = {};
    let otherForm = handleOtehrForm(formChangeText, deptSearchArr); //处理 点击的节点返回的 岗位部门,人的表单字段
    if (changeKey?.[formChangeText] && changeKey?.[formChangeText]?.length > 0 && otherForm?.USER) {
      treeObj[otherForm?.USER] = changeKey?.[formChangeText];
    } else {
      treeObj[otherForm?.USER] = [];
    }
    return treeObj;
  }
};

/**
 * @param dataList 获取组织搜索id
 * @returns
 */
const handleCheckDept = (dataList: any[]) => {
  let ids: string[] = [];
  dataList?.forEach((item: any) => {
    ids?.push(item?.value);
  });
  return ids;
};

/**处理树选中后获取用户的参数
 * @param list 传入的选中书id
 * @returns
 */
const handleGetId = (list: any[]) => {
  let paramsIDS: any = {}; //参数保存
  // let checkRow = list[0]; //点击选中的节点
  let textTypeZero: string[] = []; //保存公司参数
  let textTypeOne: string[] = []; //保存部门参数
  let textTypeTwo: string[] = []; //保存岗位参数

  // 递归树
  // let loop = (treeData: any[]) => {
  list?.forEach((item: any) => {
    // if (item?.children && item?.children?.length > 0) {
    //   loop(item?.children);
    // }
    if (item?.textType == 0) {
      textTypeZero?.push(item?.id);
    }
    if (item?.textType == 1) {
      textTypeOne?.push(item?.id);
    }
    if (item?.textType == 2) {
      textTypeTwo?.push(item?.id);
    }
  });
  // };
  // loop(list);

  if (textTypeTwo && textTypeTwo?.length > 0) {
    // return { userCompanyID: idsString };
    let idsString = textTypeTwo?.join(',');
    paramsIDS['userPositionID'] = idsString;
  }
  if (textTypeOne && textTypeOne?.length > 0) {
    // return { userDepartmentID: idsString };
    let idsString = textTypeOne?.join(',');
    paramsIDS['userDepartmentID'] = idsString;
  }
  if (textTypeZero && textTypeZero?.length > 0) {
    // return { userPositionID: idsString };
    let idsString = textTypeZero?.join(',');
    paramsIDS['userCompanyID'] = idsString;
  }
  return paramsIDS;
  // let idsString = paramsIDS?.join(',');
  // if (checkRow?.textType == 0) {
  //   return { userCompanyID: idsString };
  // } else if (checkRow?.textType == 1) {
  //   return { userDepartmentID: idsString };
  // } else if (checkRow?.textType == 2) {
  //   return { userPositionID: idsString };
  // }
};

/**处理返回的 各表单值 */
const handleOtehrForm = (objName: string, deptSearchArr: any) => {
  let otehrForm: any = {};
  deptSearchArr?.forEach((item: any) => {
    if (item?.componentType == 'DeptAndUser' && (`${item?.name}Dept` == objName || `${item?.name}User` == objName)) {
      otehrForm = item?.formOrgType;
    }
  });
  return otehrForm;
};

/**处理返回的 各表单值 */
const handleOtehrFormBool = (objName: string, deptSearchArr: any) => {
  let bool: boolean = true;
  deptSearchArr?.forEach((item: any) => {
    if (item?.componentType == 'DeptAndUser' && `${item?.name}Dept` == objName) {
      bool = true;
    }
    if (item?.componentType == 'DeptAndUser' && `${item?.name}User` == objName) {
      bool = false;
    }
  });
  return bool;
};
